package xmlparser;

import java.io.ByteArrayInputStream;
import java.util.Hashtable;
import java.util.List;
import java.util.StringTokenizer;

import javax.xml.parsers.SAXParser;
import javax.xml.parsers.SAXParserFactory;

import org.xml.sax.Attributes;
import org.xml.sax.InputSource;
import org.xml.sax.SAXException;
import org.xml.sax.XMLReader;
import org.xml.sax.helpers.DefaultHandler;

import docks.Dock;

public class BicingHandler extends DefaultHandler {

    int descriptionCounter = 0;
    String currentValue = null;
    Dock currentDock = new Dock();
    boolean in_description = false;
    boolean in_coordinates = false;
    boolean in_address = false;
    boolean in_availability = false;

    public List<Dock> dockList1;
    public Hashtable<Integer, Dock> dockList;

    @Override
    public void startDocument() throws SAXException {

	dockList = new Hashtable<Integer, Dock>();
    }

    /**
     * Called when tag starts ( ex:- <name>AndroidPeople</name> -- <name> )
     */
    @Override
    public void startElement(String uri, String localName, String qName,
	    Attributes attributes) throws SAXException {

	if (localName.equalsIgnoreCase("description")) {

	    in_description = true;
	} else if (localName.equalsIgnoreCase("coordinates")) {

	    in_coordinates = true;
	} else if (localName.equalsIgnoreCase("placemark")) {

	    currentDock = new Dock();
	}

	if (in_description) {

	    if (localName.equalsIgnoreCase("div")) {

		switch (descriptionCounter) {

		case 0:
		    in_address = true;
		    break;
		case 1:
		    // nothing to do here
		    break;
		case 2:
		    in_availability = true;
		    break;
		}
	    }
	}

    }

    /**
     * Called when tag closing ( ex:- <name>AndroidPeople</name> -- </name> )
     */
    @Override
    public void endElement(String uri, String localName, String qName)
	    throws SAXException {

	if (localName.equalsIgnoreCase("description")) {

	    in_description = false;
	} else if (localName.equalsIgnoreCase("coordinates")) {

	    in_coordinates = false;
	} else if (localName.equalsIgnoreCase("placemark")) {

	    if (currentDock.getIdDock() != -1)
		dockList.put(currentDock.getIdDock(), currentDock);
	}

	if (in_description) {

	    if (localName.equalsIgnoreCase("div")) {

		switch (descriptionCounter) {

		case 0:
		    in_address = false;
		    descriptionCounter++;
		    break;
		case 1:
		    descriptionCounter++;
		    break;
		case 2:
		    descriptionCounter = 0;
		    in_availability = false;
		    break;
		}
	    }
	}
    }

    /**
     * Called to get tag characters ( ex:- <name>AndroidPeople</name> -- to get
     * AndroidPeople Character )
     */
    @Override
    public void characters(char[] ch, int start, int length)
	    throws SAXException {

	String element;
	StringTokenizer st;

	if (in_description) {

	    String subxml = new String(ch, start, length);
	    try {  // Create a new parser to get parse this subxml
		SAXParserFactory spf = SAXParserFactory.newInstance();
		SAXParser sp = spf.newSAXParser();
		XMLReader xr = sp.getXMLReader();

		SubXmlHandler sxh = new SubXmlHandler();
		xr.setContentHandler(sxh);

		ByteArrayInputStream in = new ByteArrayInputStream(
			subxml.getBytes());
		InputSource source = new InputSource(in);
		xr.parse(source);

		currentDock.setIdDock(sxh.id);
		currentDock.setAddress(sxh.name);
		currentDock.setAvailableBikes(sxh.occupied);
		currentDock.setFreeSlots(sxh.free);

	    } catch (Exception e) {

		e.printStackTrace();
	    }
	}

	if (in_coordinates) {

	    element = new String(ch, start, length);
	    st = new StringTokenizer(element);

	    String lat = "0", lon = "0";

	    try {
		lat = st.nextToken(",");
		lon = st.nextToken();
	    } catch (Exception e) {

	    }

	    if (!lat.contains("?") && !lon.contains("?")) {

		currentDock.setLatitude(Double.valueOf(lat));
		currentDock.setLongitude(Double.valueOf(lon));
	    } else {

		currentDock.setLatitude(-1);
	    }

	}

    }
}
